home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / jam / jamdisk1 / cursor / examples / link / isprime.bas < prev    next >
Encoding:
BASIC Source File  |  1995-03-18  |  1.6 KB  |  45 lines

  1.  
  2. ' This example shows how you can link C-subprograms to BASIC-programs
  3. ' The C-program was compiled using ZC from Fish-Disk 314
  4.  
  5.  OPTION TRYCLIWINDOW
  6.  OPTION ALLPCRELATIVE
  7.  
  8.  INPUT "Number";a&
  9.  CALL IsPrime (a&),IsPrime&
  10.  
  11.  IF IsPrime& THEN
  12.    PRINT a&;" is a prime number"
  13.  ELSE
  14.    PRINT a&;" is no prime number"
  15.  END IF
  16.  
  17.  SUB EXTERNAL IsPrime (Number&,IsPrime&) STATIC
  18.  
  19. ' The SUB-command does the following things:
  20. ' - Whenever you call 'IsPrime' via the CALL-command the parameters of the
  21. '   CALL-command are converted to the types specified in the SUB-command.
  22. ' - The C or assembly program is called. It must have the name '_ISPRIME'
  23. '   (must be in upper case).
  24. '   Unlike in C-programs 'Cursor' pushes the left parameters first, not
  25. '   last. Thus the C-program must reverse the order of the parameters:
  26. '   In this case
  27. '     IsPrime(isprime,number)
  28. '   is correct, but not
  29. '     IsPrime(number,isprime).
  30. ' - At the moment it is not possible to get the result of the C-function
  31. '   (returned in D0), but the C-Program can modify the variables on the
  32. '   stack. Variables not enclosed in brackets get their new value from there
  33. '   (where they perhaps were modified by the C-program).
  34. '   In this example program the result is returned in 'IsPrime&'.
  35. ' - 'Cursor' will not create an executable program but an object file, which
  36. '   can be linked with the C-routines.
  37. '   The object file created by 'Cursor' must be the first file linked!
  38. '
  39. ' Note that the new program might not be 'pure' any more, this depends
  40. ' on the linked C or assembly language routines.
  41.  
  42. ' 'IsPrime&' is a long and not an int variable because I had problems with
  43. ' ZC and int/short variables.
  44.  
  45.